package com.nimitz.modules.approval.controller;


import com.nimitz.common.core.web.controller.BaseController;
import com.nimitz.common.core.web.domain.AjaxResult;
import com.nimitz.modules.approval.domain.ApprovalUser;
import com.nimitz.modules.approval.service.IApprovalUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ApprovalUserController
 * @Author FuCun
 * @Date 2021-08-17-14:45
 * @PackageName com.nimitz.modules.approval.controller
 * @Description 审批人员
 * @Version 1.0.0
 */
@RestController
@Api(tags = "审批人员信息")
@RequestMapping("/approval-user")
public class ApprovalUserController extends BaseController {
    @Autowired
    private IApprovalUserService approvalUserService;

    /**
     * 查询所有审批人员信息（含搜索）
     *
     * @param approvalUser 审批人员信息
     * @return 审批人员信息集合
     */
    @GetMapping("/userList")
    @ApiOperation(value = "查询所有审批人员信息（含搜索）")
    public AjaxResult list(ApprovalUser approvalUser) {
        List<ApprovalUser> approvalUsers = approvalUserService.selectApprovalUserList(approvalUser);
        System.out.println(approvalUsers);
        return AjaxResult.success(approvalUsers);
    }

    /**
     * 新增审批人员信息
     *
     * @param approvalUser 审批人员信息
     * @return 结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增审批人员信息")
    public AjaxResult add(@RequestBody ApprovalUser approvalUser){
        ApprovalUser  approvalUserCommon = approvalUserService.checkApprovalUserNameUnique(approvalUser.getUserName());
        if(approvalUserCommon!=null){
            if(approvalUserCommon.getUserName().equals(approvalUser.getUserName())){
                return AjaxResult.error("新增审批人"+approvalUser.getUserName()+"失败");
            }
        }
        return toAjax(approvalUserService.insertApprovalUser(approvalUser));
    }

    /**
     * 修改审批人员信息
     *
     * @param approvalUser 审批人员信息
     * @return 结果
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改审批人员信息")
    public AjaxResult edit(@RequestBody ApprovalUser approvalUser){

        ApprovalUser  approvalUserCommon = approvalUserService.checkApprovalUserNameUnique(approvalUser.getUserName());
        if(approvalUserCommon!=null){
            if(approvalUserCommon.getUserName().equals(approvalUser.getUserName())){
                return AjaxResult.error("修改审批人"+approvalUser.getUserName()+"失败");
            }
        }
        return toAjax(approvalUserService.updateApprovalUser(approvalUser));
    }

    /**
     * 删除审批人员
     *
     * @param approvalUserId 审批人员id
     * @return 结果
     */

    @DeleteMapping("/{approvalUserId}")
    @ApiOperation(value = "删除审批人员信息")
    public AjaxResult remove(@PathVariable Long approvalUserId) {
        return toAjax(approvalUserService.deleteApprovalUserByUserId(approvalUserId));
    }
}
